package be.xios.view;


import org.json.JSONException;
import org.json.JSONObject;

import ws.WebService;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;

public class TouchControll extends View{
	private Canvas c;
	private Paint paint;
	private Paint p;
	private int steps;
	private int x;
	private int y;
	private int xPoint;
	private int yPoint;
	private int oldx;
	private int oldy;
	private Path path;
	private boolean touch = false;
	private TouchJoystickModel tjm;
	private WebService ws;
	public boolean isTouch() {
		return touch;
	}
	public void setTouch(boolean touch) {
		this.touch = touch;
	}
	public TouchJoystickModel getTjm() {
		return tjm;
	}
	public void setTjm(TouchJoystickModel tjm) {
		this.tjm = tjm;
	}
	//http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html
	public TouchControll(Context context) {
		super(context);
		init();
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		touch = true;
		int x = (int)event.getX();
		int y = (int)event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			updatePosition(x, y);
			oldx =x;
			oldy =y;
			break;
		case MotionEvent.ACTION_MOVE:
			updatePosition(x, y);
			if((Math.abs(x-oldx)>5)||(Math.abs(y-oldy)>5)){
				try {
					ws = new WebService();
					JSONObject o = new JSONObject();
					o.put("info", tjm.getInfo());
					ws.executePut(WebService.BASE_URL+"/info", o);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				oldx =x;
				oldy =y;
			}
			break;
		case MotionEvent.ACTION_UP:
			updatePosition(-1,-1); 
			touch = false;
			break;
		}
		return true;
	}
	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(2);
		paint.setTextSize(25);
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(Color.WHITE);

		p = new Paint();
		p.setAntiAlias(true);
		p.setStrokeWidth(2);
		p.setTextSize(25);
		p.setStyle(Paint.Style.FILL);
		p.setColor(Color.WHITE);
		steps = 3;

		path = new Path();
		tjm = new TouchJoystickModel();
	}
	@Override
	protected void onDraw(Canvas canvas) {
		c = canvas;
		xPoint = getMeasuredWidth()/2;
		yPoint = getMeasuredHeight()/2;
		path.addCircle(xPoint, yPoint, 150, Path.Direction.CW);
		c.drawPath(path, paint);
		for (int i = 0; i < steps; i++) {
			c.drawLine(xPoint-5, yPoint-(i*55), xPoint+5, yPoint-(i*55), paint);
			c.drawLine(xPoint-5, yPoint+(i*55), xPoint+5, yPoint+(i*55), paint);
			c.drawLine(xPoint+(i*55), yPoint-5, xPoint+(i*55), yPoint+5, paint);
			c.drawLine(xPoint-(i*55), yPoint-5, xPoint-(i*55), yPoint+5, paint);
		}
		if(touch ==true){
			c.drawCircle(this.x,this.y,15,p);
			c.drawText("[x:"+(x-xPoint)+" | y:"+(y-yPoint)+"]", (float)(xPoint+60), (float)(yPoint-160), paint);
			c.drawText(createInfo((x-xPoint),(y-yPoint)),(float)(xPoint+60), (float)(yPoint-190), paint);
			c.drawText(speedInfo((x-xPoint),(y-yPoint)),(float)(xPoint-230), (float)(yPoint-160), paint);
		}
	}
	public String speedInfo(int x,int y){
			return"[Speed x: "+Math.abs(x/25)+"|Speed y: "+Math.abs(y/25)+"]";
	}
	public String createInfo(int x,int y){
		if(x==0&&y==0){
			tjm.setInfo("[Middle]" +speedInfo(x,y));
		}else{
			if(y<0){
				if(x<0){
					tjm.setInfo("[Top & Left]"+speedInfo(x,y));
				}else{
					tjm.setInfo("[Top & Right]"+speedInfo(x,y));		
				}
			}else{
				if(x<0){
					tjm.setInfo("[Bottom & Left]"+speedInfo(x,y));
				}else{
					tjm.setInfo("[Bottom & Right]"+speedInfo(x,y));		
				}	
			}
		}

		JSONObject o = new JSONObject();
		try {
			o.put("info", tjm.getInfo());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tjm.getInfo();


	}
	public void updatePosition(int x, int y){
		if((Math.pow((x-xPoint),2)+ Math.pow((y-yPoint),2))< Math.pow(155,2)){
			this.x= x;
			this.y = y;
		}else{
			this.x = getMeasuredWidth()/2;
			this.y = getMeasuredHeight()/2;
		}
		invalidate();		
	}
}
